Take Home Exercise 3: Predicting HDB Public Housing Resale Pricies using Geographically Weighted Methods

Author

Kwek Ming Rong

Published

March 9, 2023

Modified

March 25, 2023

Source: https://singaplex.com/magazine/singles-hdb

Background Information

Housing is an essential component of household wealth worldwide. Be it for a couple or individual, buying a house has always been a dream or goal to stay for most people. The price of housing is affected by many factors. Some of them are global in nature such as the general economy of a country or inflation rate. Others can be more specific to the properties themselves. These factors can be further divided to structural and locational factors. Structural factors are variables related to the property themselves such as the size, fitting, and tenure of the property. Locational factors are variables related to the neighbourhood of the properties such as proximity to childcare centre, public transport service and shopping centre.

Conventional, housing resale prices predictive models were built by using Ordinary Least Square (OLS) method. However, this method failed to take into consideration that spatial autocorrelation and spatial heterogeneity exist in geographic data sets such as housing transactions. With the existence of spatial autocorrelation, the OLS estimation of predictive housing resale pricing models could lead to biased, inconsistent, or inefficient results (Anselin 1998). In view of this limitation, Geographical Weighted Models were introduced for calibrating predictive model for housing resale prices.

The Objective

In this take-home exercise, I am tasked to predict HDB resale prices at the sub-market level (i.e. HDB 3-room, HDB 4-room and HDB 5-room) for the month of January and February 2023 in Singapore. The predictive models must be built by using by using conventional OLS method and GWR methods. I am also required to compare the performance of the conventional OLS method versus the geographical weighted methods.

1. Datasets

  • Aspatial dataset:

    • HDB Resale data: a list of HDB resale transacted prices in Singapore from Jan 2017 onwards. It is in csv format which can be downloaded from Data.gov.sg.
  • Geospatial dataset:

    • MP14_SUBZONE_WEB_PL: a polygon feature data providing information of URA 2014 Master Plan Planning Subzone boundary data. It is in ESRI shapefile format. This data set was also downloaded from Data.gov.sg
  • Location factors with regards to the geographic coordinates:

    • Downloaded from Data.gov.sg.

      • Eldercare data is a list of eldercare in Singapore. It is in shapefile format.

      • Hawker Centre data is a list of hawker centres in Singapore. It is in geojson format.

      • Parks data is a list of parks in Singapore. It is in geojson format.

      • Supermarket data is a list of supermarkets in Singapore. It is in geojson format.

      • CHAS clinics data is a list of CHAS clinics in Singapore. It is in geojson format.

      • Childcare service data is a list of childcare services in Singapore. It is in geojson format.

      • Kindergartens data is a list of kindergartens in Singapore. It is in geojson format.

    • Downloaded from Datamall.lta.gov.sg.

      • MRT data is a list of MRT/LRT stations in Singapore with the station names and codes. It is in shapefile format.

      • Bus stops data is a list of bus stops in Singapore. It is in shapefile format.

  • Location factors without geographic coordinates:

    • Downloaded from Data.gov.sg.

      • Primary school data is extracted from the list on General information of schools from data.gov portal. It is in csv format.
    • Retrieved/Scraped from other sources

      • CBD coordinates obtained from Google.

      • Shopping malls data is a list of Shopping malls in Singapore obtained from Wikipedia.

      • Good primary schools is a list of primary schools that are ordered in ranking in terms of popularity and this can be found at Local Salary Forum.

2. Loading the R packages

The following code chunk will perform the following task. A list called packages will be created and will consists of all the R packages required to accomplish this exercise. There will be a check to see if R packages on package have been installed in R and if not, they will be installed. After which when all the R packages have been installed, the packages will then be loaded

packages <- c('sf', 'tidyverse', 'tmap', 'httr', 'jsonlite', 'rvest', 
              'sp', 'ggpubr', 'corrplot', 'broom',  'olsrr', 'spdep', 
              'GWmodel', 'devtools', 'rgeos', 'lwgeom', 'maptools', 'rsample', 'Metrics', 'SpatialML')

for(p in packages){
  if(!require(p, character.only = T)){
    install.packages(p, repos = "http://cran.us.r-project.org")
  }
  library(p, character.only = T)
}
devtools::install_github("gadenbuie/xaringanExtra")
library(xaringanExtra)

More on the packages used:

  • sf: used for importing, managing, and processing geospatial data

  • tidyverse: used for importing, wrangling and visualising data. It consists of a family of R packages, such as:

    • readr for importing csv data,

    • readxl for importing Excel worksheet,

    • tidyr for manipulating data,

    • dplyr for transforming data, and

    • ggplot2 for visualising data

  • tmap: provides functions for plotting cartographic quality static inpoint patterns maps or interactive maps by using leaflet API.

  • httr: Useful tools for working with HTTP organised by HTTP verbs (GET(), POST(), etc). Configuration functions make it easy to control additional request components (authenticate(), add_headers() and so on).

    • In this analysis, it will be used to send GET requests to OneMapAPI SG to retrieve the coordinates of addresses.
  • jsonlite: A simple and robust JSON parser and generator for R. It offers simple, flexible tools for working with JSON in R, and is particularly powerful for building pipelines and interacting with a web API.

  • rvest: A new package that makes it easy to scrape (or harvest) data from html web pages, inspired by libraries like beautiful soup.

    • In this analysis, it will be used to scrape data for shopping malls and good primary schools
  • sp: provides classes and methods for dealing with spatial data in R.

  • ggpubr: provides some easy-to-use functions for creating and customizing ggplot2 based publication ready plots

    • In this analysis, it will be used to arrange multiple ggplots.
  • corrplot: For Multivariate data visualisation and analysis

  • broom: Takes the messy output of built-in functions in R, such as lm, nls, or t.test, and turns them into tidy tibble.

    • In this analysis, functions like tidy and glance will be used to construct a tibble / summmary of the model which is easier to look at.
  • oslrr: Used to build OLD and performing diagnostic tests.

  • spdep: For spatial dependence statistics.

  • GWmodel: Calibrate geographical weighted family of modes.

  • devtools: used for installing any R packages which is not available in RCRAN. In this exercise, I will be installing using devtools to install the package xaringanExtra which is still under development stage.

  • xaringanExtra: is an enhancement of xaringan package. As it is still under development stage, we can still install the current version using install_github function of devtools. This package will be used to add Panelsets to contain both the r code chunk and results whereever applicable.

3. Importing of Aspatial Data & Wrangling

read_csv() function of readr package will be used to import resale-flat-prices into R as a tibble data frame called resale. glimpse() function of dplyr package is used to display the data structure

resale <- read_csv("data/aspatial/resale-flat-prices.csv")
glimpse(resale)
Rows: 148,373
Columns: 11
$ month               <chr> "2017-01", "2017-01", "2017-01", "2017-01", "2017-…
$ town                <chr> "ANG MO KIO", "ANG MO KIO", "ANG MO KIO", "ANG MO …
$ flat_type           <chr> "2 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", …
$ block               <chr> "406", "108", "602", "465", "601", "150", "447", "…
$ street_name         <chr> "ANG MO KIO AVE 10", "ANG MO KIO AVE 4", "ANG MO K…
$ storey_range        <chr> "10 TO 12", "01 TO 03", "01 TO 03", "04 TO 06", "0…
$ floor_area_sqm      <dbl> 44, 67, 67, 68, 67, 68, 68, 67, 68, 67, 68, 67, 67…
$ flat_model          <chr> "Improved", "New Generation", "New Generation", "N…
$ lease_commence_date <dbl> 1979, 1978, 1980, 1980, 1980, 1981, 1979, 1976, 19…
$ remaining_lease     <chr> "61 years 04 months", "60 years 07 months", "62 ye…
$ resale_price        <dbl> 232000, 250000, 262000, 265000, 265000, 275000, 28…

When we load in the dataset for the first time, we can see that:

The dataset contains 11 columns with 148,373 rows. The columns that are present in the data are: month, town, flat_type, block, street_name, storey_range, floor_area_sqm, flat_model, lease_commence_date, remaining_lease, resale_price.

For this take home exercise 3, we are allowed the option to choose to perform our analysis between either 3, 4 or 5 room flat transactions. Therefore, I will be selecting the 3 room flat transactions during the transaction period from 1st January 2021 to 31st December 2022. Test data should be included for January and February 2023 resale prices.

3.1 Filtering HDB Resale Data

filter() function of dplyr package will be used to select the desired flat_type and dates which will be stored in rs_subset.

rs_subset <-  filter(resale,flat_type == "3 ROOM") %>% 
              filter(month >= "2021-01" & month <= "2023-02")

unique() function of R package will be used to check whether flat_type and month have been extracted successfully.

unique(rs_subset$month)
 [1] "2021-01" "2021-02" "2021-03" "2021-04" "2021-05" "2021-06" "2021-07"
 [8] "2021-08" "2021-09" "2021-10" "2021-11" "2021-12" "2022-01" "2022-02"
[15] "2022-03" "2022-04" "2022-05" "2022-06" "2022-07" "2022-08" "2022-09"
[22] "2022-10" "2022-11" "2022-12" "2023-01" "2023-02"
unique(rs_subset$flat_type)
[1] "3 ROOM"

glimpse() function will be used to view the overall resale transactions available for 3 room flat in Singapore.

glimpse(rs_subset)
Rows: 13,780
Columns: 11
$ month               <chr> "2021-01", "2021-01", "2021-01", "2021-01", "2021-…
$ town                <chr> "ANG MO KIO", "ANG MO KIO", "ANG MO KIO", "ANG MO …
$ flat_type           <chr> "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", …
$ block               <chr> "331", "534", "561", "170", "463", "542", "170", "…
$ street_name         <chr> "ANG MO KIO AVE 1", "ANG MO KIO AVE 10", "ANG MO K…
$ storey_range        <chr> "04 TO 06", "04 TO 06", "01 TO 03", "07 TO 09", "0…
$ floor_area_sqm      <dbl> 68, 68, 68, 60, 68, 68, 60, 73, 67, 67, 68, 68, 73…
$ flat_model          <chr> "New Generation", "New Generation", "New Generatio…
$ lease_commence_date <dbl> 1981, 1980, 1980, 1986, 1980, 1981, 1986, 1976, 19…
$ remaining_lease     <chr> "59 years", "58 years 02 months", "58 years 01 mon…
$ resale_price        <dbl> 260000, 265000, 265000, 268000, 268000, 270000, 27…

From the glimpse() function result, we can see that from Jan 2021 to December 2022, there are 23,656 transactions for 3 room flat in Singapore.

3.2 Transforming HDB Resale Data Columns

Here, mutate function of dplyr package will be used to create columns such as:

  • address: concatenation of the block and street_name columns using paste() function of base R package. remaining_lease_yr & remaining_lease_mth: split the year and months part of the remaining_lease respectively using str_sub() function of stringr package then converting the character to integer using as.integer() function of base R package. After performing mutate function, we will store the new data in rs_transform
rs_transform <- rs_subset %>%
  mutate(rs_subset, address = paste(block,street_name)) %>%
  mutate(rs_subset, remaining_lease_yr = as.integer(str_sub(remaining_lease, 0, 2)))%>%
  mutate(rs_subset, remaining_lease_mth = as.integer(str_sub(remaining_lease, 9, 11)))
head(rs_transform)
# A tibble: 6 × 14
  month   town     flat_…¹ block stree…² store…³ floor…⁴ flat_…⁵ lease…⁶ remai…⁷
  <chr>   <chr>    <chr>   <chr> <chr>   <chr>     <dbl> <chr>     <dbl> <chr>  
1 2021-01 ANG MO … 3 ROOM  331   ANG MO… 04 TO …      68 New Ge…    1981 59 yea…
2 2021-01 ANG MO … 3 ROOM  534   ANG MO… 04 TO …      68 New Ge…    1980 58 yea…
3 2021-01 ANG MO … 3 ROOM  561   ANG MO… 01 TO …      68 New Ge…    1980 58 yea…
4 2021-01 ANG MO … 3 ROOM  170   ANG MO… 07 TO …      60 Improv…    1986 64 yea…
5 2021-01 ANG MO … 3 ROOM  463   ANG MO… 04 TO …      68 New Ge…    1980 58 yea…
6 2021-01 ANG MO … 3 ROOM  542   ANG MO… 04 TO …      68 New Ge…    1981 59 yea…
# … with 4 more variables: resale_price <dbl>, address <chr>,
#   remaining_lease_yr <int>, remaining_lease_mth <int>, and abbreviated
#   variable names ¹​flat_type, ²​street_name, ³​storey_range, ⁴​floor_area_sqm,
#   ⁵​flat_model, ⁶​lease_commence_date, ⁷​remaining_lease

3.2.1 Sum remaining lease in months

There are some NA values in remaining lease months with value of 0. We need to multiply the remaining_lease_yr by 12 to convert into months. The remaining_lease_mths column will be created using mutate function of dplyr package which contains the summation of the remaining_lease_yr and remaining_lease_mths using rowSums() of R package.

rs_transform$remaining_lease_mth[is.na(rs_transform$remaining_lease_mth)] <- 0
rs_transform$remaining_lease_yr <- rs_transform$remaining_lease_yr * 12
rs_transform <- rs_transform %>% 
  mutate(rs_transform, remaining_lease_mths = rowSums(rs_transform[, c("remaining_lease_yr", "remaining_lease_mth")])) %>%
  select(month, town, address, block, street_name, flat_type, storey_range, floor_area_sqm, flat_model, 
         lease_commence_date, remaining_lease_mths, resale_price)
head(rs_transform)
# A tibble: 6 × 12
  month   town     address block stree…¹ flat_…² store…³ floor…⁴ flat_…⁵ lease…⁶
  <chr>   <chr>    <chr>   <chr> <chr>   <chr>   <chr>     <dbl> <chr>     <dbl>
1 2021-01 ANG MO … 331 AN… 331   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1981
2 2021-01 ANG MO … 534 AN… 534   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1980
3 2021-01 ANG MO … 561 AN… 561   ANG MO… 3 ROOM  01 TO …      68 New Ge…    1980
4 2021-01 ANG MO … 170 AN… 170   ANG MO… 3 ROOM  07 TO …      60 Improv…    1986
5 2021-01 ANG MO … 463 AN… 463   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1980
6 2021-01 ANG MO … 542 AN… 542   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1981
# … with 2 more variables: remaining_lease_mths <dbl>, resale_price <dbl>, and
#   abbreviated variable names ¹​street_name, ²​flat_type, ³​storey_range,
#   ⁴​floor_area_sqm, ⁵​flat_model, ⁶​lease_commence_date

3.3 Retrieval of Addresses & its Coordinates

Data such as postal codes and coordinates of the addresses is required to get the proximity to various location factors later on.

3.3.1 Creaion of unique list of addresses

Unique addresses will be created and stored in add_list. unique() function of base R package will be used to extract the unique addresses and sort() function of R package to sort the unique vector.

add_list <- sort(unique(rs_transform$address))

3.3.2 Creation of function to retrieve Coordinates from OneMapSG API

A dataframe postal_coords will be created to store all final retrieved coordinates. The GET() function of httr package will be used to perform a GET request to https://developers.onemap.sg/commonapi/search. There are a few search arguments variables and information we have to take note of

  • searchVal: Unique keywords that user will enter to filter results

  • returnGeom {Y/N}: Yes or No to check if user want to return the geometry

  • getAddrDetails {Y/N}: Yes or No to check if user want to return address details for a point

Return JSON response will contain many fields but we are only interested in postal code and coordinates like Longitude and Latitude. A new dataframe new_row will be created and is used to store each final set of coordinates retrieved. There is also the need to check the number of responses because some searched location have 0, some only have 1 result and others have many. Finally, the JSON result will be appended to the dataframe postal_coords using rbind() function of R.

get_coords <- function(add_list){
  
  # Create a data frame to store all retrieved coordinates
  postal_coords <- data.frame()
    
  for (i in add_list){
    #print(i)

    r <- GET('https://developers.onemap.sg/commonapi/search?',
           query=list(searchVal=i,
                     returnGeom='Y',
                     getAddrDetails='Y'))
    data <- fromJSON(rawToChar(r$content))
    found <- data$found
    res <- data$results
    
    # Create a new data frame for each address
    new_row <- data.frame()
    
    # If single result, append 
    if (found == 1){
      postal <- res$POSTAL 
      lat <- res$LATITUDE
      lng <- res$LONGITUDE
      new_row <- data.frame(address= i, postal = postal, latitude = lat, longitude = lng)
    }
    
    # If multiple results, drop NIL and append top 1
    else if (found > 1){
      # Remove those with NIL as postal
      res_sub <- res[res$POSTAL != "NIL", ]
      
      # Set as NA first if no Postal
      if (nrow(res_sub) == 0) {
          new_row <- data.frame(address= i, postal = NA, latitude = NA, longitude = NA)
      }
      
      else{
        top1 <- head(res_sub, n = 1)
        postal <- top1$POSTAL 
        lat <- top1$LATITUDE
        lng <- top1$LONGITUDE
        new_row <- data.frame(address= i, postal = postal, latitude = lat, longitude = lng)
      }
    }

    else {
      new_row <- data.frame(address= i, postal = NA, latitude = NA, longitude = NA)
    }
    
    # Add the row
    postal_coords <- rbind(postal_coords, new_row)
  }
  return(postal_coords)
}

3.3.3 Retrieve resale coordinates using the get_coords function

coords <- get_coords(add_list)

3.3.4 Check results

Check if columns contain any Nil or NA values with is.na() function of R

coords[(is.na(coords$postal) | is.na(coords$latitude) | is.na(coords$longitude) | coords$postal=="NIL"), ]
[1] address   postal    latitude  longitude
<0 rows> (or 0-length row.names)

From the result, it can be seen that 215 CHOA CHU KANG CTRL has nil postal code but there are coordinates available. When performing a search on gothere.sg, the postal code should be 680215.

3.3.5 Combination of HDB resale and coordinate data

After retrieving the coordinates, we need to combine with the HDB resale dataset using left_join() function of dplyr package. The data will be stored in rs_coords.

rs_coords <- left_join(rs_transform, coords, by = c('address' = 'address'))
head(rs_coords)
# A tibble: 6 × 15
  month   town     address block stree…¹ flat_…² store…³ floor…⁴ flat_…⁵ lease…⁶
  <chr>   <chr>    <chr>   <chr> <chr>   <chr>   <chr>     <dbl> <chr>     <dbl>
1 2021-01 ANG MO … 331 AN… 331   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1981
2 2021-01 ANG MO … 534 AN… 534   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1980
3 2021-01 ANG MO … 561 AN… 561   ANG MO… 3 ROOM  01 TO …      68 New Ge…    1980
4 2021-01 ANG MO … 170 AN… 170   ANG MO… 3 ROOM  07 TO …      60 Improv…    1986
5 2021-01 ANG MO … 463 AN… 463   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1980
6 2021-01 ANG MO … 542 AN… 542   ANG MO… 3 ROOM  04 TO …      68 New Ge…    1981
# … with 5 more variables: remaining_lease_mths <dbl>, resale_price <dbl>,
#   postal <chr>, latitude <chr>, longitude <chr>, and abbreviated variable
#   names ¹​street_name, ²​flat_type, ³​storey_range, ⁴​floor_area_sqm,
#   ⁵​flat_model, ⁶​lease_commence_date

3.4 Writing file to rds

rs_coords_rds <- write_rds(rs_coords, "Data/rds/rs_coords.rds")

3.5 Reading the rs_coords_rds file

rs_coords <- read_rds("Data/rds/rs_coords.rds")
glimpse(rs_coords)
Rows: 13,780
Columns: 15
$ month                <chr> "2021-01", "2021-01", "2021-01", "2021-01", "2021…
$ town                 <chr> "ANG MO KIO", "ANG MO KIO", "ANG MO KIO", "ANG MO…
$ address              <chr> "331 ANG MO KIO AVE 1", "534 ANG MO KIO AVE 10", …
$ block                <chr> "331", "534", "561", "170", "463", "542", "170", …
$ street_name          <chr> "ANG MO KIO AVE 1", "ANG MO KIO AVE 10", "ANG MO …
$ flat_type            <chr> "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM", "3 ROOM",…
$ storey_range         <chr> "04 TO 06", "04 TO 06", "01 TO 03", "07 TO 09", "…
$ floor_area_sqm       <dbl> 68, 68, 68, 60, 68, 68, 60, 73, 67, 67, 68, 68, 7…
$ flat_model           <chr> "New Generation", "New Generation", "New Generati…
$ lease_commence_date  <dbl> 1981, 1980, 1980, 1986, 1980, 1981, 1986, 1976, 1…
$ remaining_lease_mths <dbl> 708, 698, 697, 770, 698, 709, 768, 652, 682, 664,…
$ resale_price         <dbl> 260000, 265000, 265000, 268000, 268000, 270000, 2…
$ postal               <chr> "560331", "560534", "560561", "560170", "560463",…
$ latitude             <chr> "1.36211140665392", "1.37405846295585", "1.370577…
$ longitude            <chr> "103.850766473498", "103.854168170426", "103.8578…

3.5.1 Transform and Assign CRS

The Latitude & Longitude are in decimal, therefore the projected CRS will be WGS84. We will need to assign the CRS of 4326 first before transforming it to 3414 which is the EPSG code for Singapore SVY21

rs_coords_sf <- st_as_sf(rs_coords,
                    coords = c("longitude", 
                               "latitude"),
                    crs=4326) %>%
  st_transform(crs = 3414)
st_crs(rs_coords_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]

3.5.2 Plotting HDB resale points

tmap_mode("view")
tm_shape(rs_coords_sf)+
  tm_dots(col="blue", size = 0.02)
tmap_mode("plot")

4. Importing Geospatial Locational Factors

4.1 Location factors with coordinates

4.1.1 Reading of location factors

elder_sf <- st_read(dsn = "Data/Geospatial", layer="ELDERCARE")
Reading layer `ELDERCARE' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 133 features and 18 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 14481.92 ymin: 28218.43 xmax: 41665.14 ymax: 46804.9
Projected CRS: SVY21
mrtlrt_sf <- st_read(dsn = "Data/Geospatial", layer="MRTLRTStnPtt")
Reading layer `MRTLRTStnPtt' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 185 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 6138.311 ymin: 27555.06 xmax: 45254.86 ymax: 47854.2
Projected CRS: SVY21
bus_sf <- st_read(dsn = "Data/Geospatial", layer="BusStop")
Reading layer `BusStop' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 5159 features and 3 fields
Geometry type: POINT
Dimension:     XY
Bounding box:  xmin: 3970.122 ymin: 26482.1 xmax: 48284.56 ymax: 52983.82
Projected CRS: SVY21
hawker_sf <- st_read("Data/Geospatial/hawker-centres-geojson.geojson") 
Reading layer `hawker-centres-geojson' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\hawker-centres-geojson.geojson' 
  using driver `GeoJSON'
Simple feature collection with 125 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6974 ymin: 1.272716 xmax: 103.9882 ymax: 1.449217
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
supermkt_sf <- st_read("Data/Geospatial/supermarkets-geojson.geojson") 
Reading layer `supermarkets-geojson' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\supermarkets-geojson.geojson' 
  using driver `GeoJSON'
Simple feature collection with 526 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6258 ymin: 1.24715 xmax: 104.0036 ymax: 1.461526
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
chas_sf <- st_read("Data/Geospatial/moh-chas-clinics.geojson")
Reading layer `moh-chas-clinics' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\moh-chas-clinics.geojson' 
  using driver `GeoJSON'
Simple feature collection with 1064 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.5818 ymin: 1.016264 xmax: 103.9903 ymax: 1.456037
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
childcare_sf <- st_read("Data/Geospatial/childcare.geojson") 
Reading layer `childcare' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\childcare.geojson' 
  using driver `GeoJSON'
Simple feature collection with 1545 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6824 ymin: 1.248403 xmax: 103.9897 ymax: 1.462134
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
kind_sf <- st_read("Data/Geospatial/preschools-location.geojson") 
Reading layer `preschools-location' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\preschools-location.geojson' 
  using driver `GeoJSON'
Simple feature collection with 1925 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6824 ymin: 1.247759 xmax: 103.9897 ymax: 1.462134
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
parks_sf <- st_read("Data/Geospatial/parks-geojson.geojson")
Reading layer `parks-geojson' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial\parks-geojson.geojson' 
  using driver `GeoJSON'
Simple feature collection with 350 features and 2 fields
Geometry type: POINT
Dimension:     XYZ
Bounding box:  xmin: 103.6929 ymin: 1.214058 xmax: 104.0017 ymax: 1.461503
z_range:       zmin: 0 zmax: 0
Geodetic CRS:  WGS 84
st_crs(elder_sf)
Coordinate Reference System:
  User input: SVY21 
  wkt:
PROJCRS["SVY21",
    BASEGEOGCRS["SVY21[WGS84]",
        DATUM["World Geodetic System 1984",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ID["EPSG",6326]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["Degree",0.0174532925199433]]],
    CONVERSION["unnamed",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]
st_crs(mrtlrt_sf)
Coordinate Reference System:
  User input: SVY21 
  wkt:
PROJCRS["SVY21",
    BASEGEOGCRS["SVY21[WGS84]",
        DATUM["World Geodetic System 1984",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ID["EPSG",6326]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["Degree",0.0174532925199433]]],
    CONVERSION["unnamed",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]
st_crs(bus_sf)
Coordinate Reference System:
  User input: SVY21 
  wkt:
PROJCRS["SVY21",
    BASEGEOGCRS["WGS 84",
        DATUM["World Geodetic System 1984",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]],
            ID["EPSG",6326]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["Degree",0.0174532925199433]]],
    CONVERSION["unnamed",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["Degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["(E)",east,
            ORDER[1],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]],
        AXIS["(N)",north,
            ORDER[2],
            LENGTHUNIT["metre",1,
                ID["EPSG",9001]]]]
st_crs(hawker_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(supermkt_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(chas_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(childcare_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(kind_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]
st_crs(parks_sf)
Coordinate Reference System:
  User input: WGS 84 
  wkt:
GEOGCRS["WGS 84",
    DATUM["World Geodetic System 1984",
        ELLIPSOID["WGS 84",6378137,298.257223563,
            LENGTHUNIT["metre",1]]],
    PRIMEM["Greenwich",0,
        ANGLEUNIT["degree",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS["geodetic latitude (Lat)",north,
            ORDER[1],
            ANGLEUNIT["degree",0.0174532925199433]],
        AXIS["geodetic longitude (Lon)",east,
            ORDER[2],
            ANGLEUNIT["degree",0.0174532925199433]],
    ID["EPSG",4326]]

4.1.2 Assign EPSG code to sf dataframes

elder_sf <- st_set_crs(elder_sf, 3414)
mrtlrt_sf <- st_set_crs(mrtlrt_sf, 3414)
bus_sf <- st_set_crs(bus_sf, 3414)
hawker_sf <- hawker_sf %>%
  st_transform(crs = 3414)
supermkt_sf <- supermkt_sf %>%
  st_transform(crs = 3414)
chas_sf <- chas_sf %>%
  st_transform(crs = 3414)
childcare_sf <- childcare_sf %>%
  st_transform(crs = 3414)
kind_sf <- kind_sf %>%
  st_transform(crs = 3414)
parks_sf <- parks_sf %>%
  st_transform(crs = 3414)
st_crs(elder_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(mrtlrt_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(bus_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(hawker_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(supermkt_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(chas_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(childcare_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
st_crs(kind_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]
#st_crs(parks_sf)
length(which(st_is_valid(elder_sf) == FALSE))
[1] 0
length(which(st_is_valid(mrtlrt_sf) == FALSE))
[1] 0
length(which(st_is_valid(hawker_sf) == FALSE))
[1] 0
length(which(st_is_valid(parks_sf) == FALSE))
[1] 0
length(which(st_is_valid(supermkt_sf) == FALSE))
[1] 0
length(which(st_is_valid(chas_sf) == FALSE))
[1] 0
length(which(st_is_valid(childcare_sf) == FALSE))
[1] 0
length(which(st_is_valid(kind_sf) == FALSE))
[1] 0
length(which(st_is_valid(bus_sf) == FALSE))
[1] 0

4.1.3 Proximity Calculation of location

get_prox function will be created to perform calculation of distances between the HDB resale and location factors

get_prox <- function(origin_df, dest_df, col_name){
  
  # creates a matrix of distances
  dist_matrix <- st_distance(origin_df, dest_df)           
  
  # find the nearest location_factor and create new data frame
  near <- origin_df %>% 
    mutate(PROX = apply(dist_matrix, 1, function(x) min(x)) / 1000) 
  
  # rename column name according to input parameter
  names(near)[names(near) == 'PROX'] <- col_name

  return(near)
}

get_prox function will be called to get the proximity of the resale HDB and location factors such as:

  • Eldercare

  • MRT

  • Hawker

  • Parks

  • Supermarkets

  • CHAS clinics

rs_coords_sf <- get_prox(rs_coords_sf, elder_sf, "PROX_ELDERLYCARE") 
rs_coords_sf <- get_prox(rs_coords_sf, mrtlrt_sf, "PROX_MRT") 
rs_coords_sf <- get_prox(rs_coords_sf, hawker_sf, "PROX_HAWKER") 
rs_coords_sf <- get_prox(rs_coords_sf, parks_sf, "PROX_PARK") 
rs_coords_sf <- get_prox(rs_coords_sf, supermkt_sf, "PROX_SUPERMARKET")
rs_coords_sf <- get_prox(rs_coords_sf, chas_sf, "PROX_CHAS")

4.1.4 Calculation of location factors within distance

get_within function will create a matrix of distances between the HDB and the location factor using st_distance of sf package. It will also get the sum of points of the location factor that are within the threshold distance using sum function of base R package then add it to HDB resale data under a new column using mutate() function of dpylr package. Lastly, it will rename the column name according to input given by user so that the columns have appropriate and distinct names that are different from one another.

get_within <- function(origin_df, dest_df, threshold_dist, col_name){
  
  # creates a matrix of distances
  dist_matrix <- st_distance(origin_df, dest_df)   
  
  # count the number of location_factors within threshold_dist and create new data frame
  wdist <- origin_df %>% 
    mutate(WITHIN_DT = apply(dist_matrix, 1, function(x) sum(x <= threshold_dist)))
  
  # rename column name according to input parameter
  names(wdist)[names(wdist) == 'WITHIN_DT'] <- col_name

  # Return df
  return(wdist)
}

4.1.4.1 Calling the function for all location factors

In this case, the threshold we set it to will be Within 350m for location factors such as, kindergartens, childcare and bus stops.

rs_coords_sf <- get_within(rs_coords_sf, kind_sf, 350, "WITHIN_350M_KINDERGARTEN")
rs_coords_sf <- get_within(rs_coords_sf, childcare_sf, 350, "WITHIN_350M_CHILDCARE")
rs_coords_sf <- get_within(rs_coords_sf, bus_sf, 350, "WITHIN_350M_BUS")

4.2 Location factors without coordinates

4.2.1 CBD Town Area

Central Business District is an area where majority of the citizens travel to daily for work. Therefore it is an important measure. With a quick google search, the latitude and longitude of Downtown Core also known as CBD, are 1.287953 and 103.851784 respectively.

# Store CBD Coordinates in Dataframe
name <- c('CBD Area')
latitude= c(1.287953)
longitude= c(103.851784)
cbd_coords <- data.frame(name, latitude, longitude)

Conversion of data frame into sf object and perform transformation of crs

cbd_coords_sf <- st_as_sf(cbd_coords,
                    coords = c("longitude", 
                               "latitude"),
                    crs=4326) %>%
  st_transform(crs = 3414)

st_crs(cbd_coords_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]

get_prox function will be called to get the proximity between the HDB resale flats and CBD area

rs_coords_sf <- get_prox(rs_coords_sf, cbd_coords_sf, "PROX_CBD")

4.2.2 Shopping Malls Places

There are multiple lists of shopping malls classified by the regions in Singapore, we are able to check the XPaths of the various lists on the wikipedia page.

url <- "https://en.wikipedia.org/wiki/List_of_shopping_malls_in_Singapore"
malls_list <- list()

for (i in 1:7){
  malls <- read_html(url) %>%
    html_nodes(xpath = paste('//*[@id="mw-content-text"]/div[1]/div[',as.character(i),']/ul/li',sep="") ) %>%
    html_text()
  malls_list <- append(malls_list, malls)
}

#malls_list

From the malls_list result, there are 164 shopping malls. As these malls does not have their coordinates, we need to use the get_coords function created previously to search the names of these shopping malls.

malls_list_coords <- get_coords(malls_list) %>% 
  rename("mall_name" = "address")
malls_list_coords <- subset(malls_list_coords, mall_name!= "Yew Tee Shopping Centre")

We need to correct the mall names that are no longer relevant and also malls with an index beside it due to Wikipedia page numbering as shown above output

invalid_malls<- subset(malls_list_coords, is.na(malls_list_coords$postal))
invalid_malls_list <- unique(invalid_malls$mall_name)
corrected_malls <- c("Clarke Quay", "City Gate", "Raffles Holland V", "Knightsbridge", "Mustafa Centre", "GR.ID", "Shaw House",
                     "The Poiz Centre", "Velocity @ Novena Square", "Singapore Post Centre", "PLQ Mall", "KINEX", "The Grandstand")

for (i in 1:length(invalid_malls_list)) {
  malls_list_coords <- malls_list_coords %>% 
    mutate(mall_name = ifelse(as.character(mall_name) == invalid_malls_list[i], corrected_malls[i], as.character(mall_name)))
}
malls_list <- sort(unique(malls_list_coords$mall_name))
malls_coords <- get_coords(malls_list)

To check if the malls still have any NA values

malls_coords[(is.na(malls_coords$postal) | is.na(malls_coords$latitude) | is.na(malls_coords$longitude)), ]
[1] address   postal    latitude  longitude
<0 rows> (or 0-length row.names)

To convert the mall dataframe into SF object and perform transformation of CRS

malls_sf <- st_as_sf(malls_coords,
                    coords = c("longitude", 
                               "latitude"),
                    crs=4326) %>%
  st_transform(crs = 3414)

get_prox function will be called to get the proximity between the HDB resale flats and Shopping Malls

rs_coords_sf <- get_prox(rs_coords_sf, malls_sf, "PROX_MALL") 

4.2.3 Primary Schools

To extract and retrieve only primary schools data

pri_sch <- read_csv("Data/Aspatial/general-information-of-schools.csv")

pri_sch <- pri_sch %>%
  filter(mainlevel_code == "PRIMARY") %>%
  select(school_name, address, postal_code, mainlevel_code)

glimpse(pri_sch)
Rows: 183
Columns: 4
$ school_name    <chr> "ADMIRALTY PRIMARY SCHOOL", "AHMAD IBRAHIM PRIMARY SCHO…
$ address        <chr> "11   WOODLANDS CIRCLE", "10   YISHUN STREET 11", "100 …
$ postal_code    <chr> "738907", "768643", "579646", "159016", "544969", "5697…
$ mainlevel_code <chr> "PRIMARY", "PRIMARY", "PRIMARY", "PRIMARY", "PRIMARY", …

There are 183 Primary Schools in Singapore.

To create a list to store postal code of the schools and retrieve the coordinates

prisch_list <- sort(unique(pri_sch$postal_code))
prisch_coords <- get_coords(prisch_list)

To check if the primary schools still have any NA values

prisch_coords[(is.na(prisch_coords$postal) | is.na(prisch_coords$latitude) | is.na(prisch_coords$longitude)), ]
[1] address   postal    latitude  longitude
<0 rows> (or 0-length row.names)

To combine coordinates with the schools

prisch_coords = prisch_coords[c("postal","latitude", "longitude")]
pri_sch <- left_join(pri_sch, prisch_coords, by = c('postal_code' = 'postal'))

To convert primary schools dataframe into SF object and perform transformation of CRS

prisch_sf <- st_as_sf(pri_sch,
                    coords = c("longitude", 
                               "latitude"),
                    crs=4326) %>%
  st_transform(crs = 3414)

To call get_within function to obtain schools within a proximity of 1km

rs_coords_sf <- get_within(rs_coords_sf, prisch_sf, 1000, "WITHIN_1KM_PRISCH")

4.2.4 Proximity to Good Primary Schools

To identify good primary schools in Singapore, one particular forum that we can use is www.salary.sg where it provides ranking of schools according to their popularity. Similarly, we can reuse the code that we extract the shopping malls in Singapore to extract the ranking of primary schools.

url <- "https://www.salary.sg/2021/best-primary-schools-2021-by-popularity/"

good_pri <- data.frame()

schools <- read_html(url) %>%
  html_nodes(xpath = paste('//*[@id="post-3068"]/div[3]/div/div/ol/li') ) %>%
  html_text() 

for (i in (schools)){
  sch_name <- toupper(gsub(" – .*","",i))
  sch_name <- gsub("\\(PRIMARY SECTION)","",sch_name)
  sch_name <- trimws(sch_name)
  new_row <- data.frame(pri_sch_name=sch_name)
  # Add the row
  good_pri <- rbind(good_pri, new_row)
}

top_good_pri <- head(good_pri, 10)

head(top_good_pri)
                         pri_sch_name
1     CHIJ ST. NICHOLAS GIRLS’ SCHOOL
2                      AI TONG SCHOOL
3                CATHOLIC HIGH SCHOOL
4                       ROSYTH SCHOOL
5 PEI HWA PRESBYTERIAN PRIMARY SCHOOL
6              NANYANG PRIMARY SCHOOL

To check if the extracted top primary schools’ name matches the existing names in the primary school dataframe

top_good_pri$pri_sch_name[!top_good_pri$pri_sch_name %in% prisch_sf$school_name]
[1] "CHIJ ST. NICHOLAS GIRLS’ SCHOOL" "CATHOLIC HIGH SCHOOL"           
[3] "ST. HILDA’S PRIMARY SCHOOL"     
#Unique list to store good school names
good_pri_list <- unique(top_good_pri$pri_sch_name)

To obtain the coordinates of good primary schools

goodprisch_coords <- get_coords(good_pri_list)

To check for NA values

goodprisch_coords[(is.na(goodprisch_coords$postal) | is.na(goodprisch_coords$latitude) | is.na(goodprisch_coords$longitude)), ]
                      address postal latitude longitude
10 ST. HILDA’S PRIMARY SCHOOL   <NA>     <NA>      <NA>

Replacing the names to match the primary school dataframe names

top_good_pri$pri_sch_name[top_good_pri$pri_sch_name == "CHIJ ST. NICHOLAS GIRLS’ SCHOOL"] <- "CHIJ SAINT NICHOLAS GIRLS' SCHOOL"
top_good_pri$pri_sch_name[top_good_pri$pri_sch_name == "ST. HILDA’S PRIMARY SCHOOL"] <- "SAINT HILDA'S PRIMARY SCHOOL"

#Unique list to store good school names
good_pri_list <- unique(top_good_pri$pri_sch_name)

#To obtain the coordinates of good primary schools
goodprisch_coords <- get_coords(good_pri_list)

Final inspection to check for NA values

goodprisch_coords[(is.na(goodprisch_coords$postal) | is.na(goodprisch_coords$latitude) | is.na(goodprisch_coords$longitude)), ]
[1] address   postal    latitude  longitude
<0 rows> (or 0-length row.names)

To convert the good primary schools dataframe into SF object and perform transformation of CRS

goodpri_sf <- st_as_sf(goodprisch_coords,
                    coords = c("longitude", 
                               "latitude"),
                    crs=4326) %>%
  st_transform(crs = 3414)

st_crs(goodpri_sf)
Coordinate Reference System:
  User input: EPSG:3414 
  wkt:
PROJCRS["SVY21 / Singapore TM",
    BASEGEOGCRS["SVY21",
        DATUM["SVY21",
            ELLIPSOID["WGS 84",6378137,298.257223563,
                LENGTHUNIT["metre",1]]],
        PRIMEM["Greenwich",0,
            ANGLEUNIT["degree",0.0174532925199433]],
        ID["EPSG",4757]],
    CONVERSION["Singapore Transverse Mercator",
        METHOD["Transverse Mercator",
            ID["EPSG",9807]],
        PARAMETER["Latitude of natural origin",1.36666666666667,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8801]],
        PARAMETER["Longitude of natural origin",103.833333333333,
            ANGLEUNIT["degree",0.0174532925199433],
            ID["EPSG",8802]],
        PARAMETER["Scale factor at natural origin",1,
            SCALEUNIT["unity",1],
            ID["EPSG",8805]],
        PARAMETER["False easting",28001.642,
            LENGTHUNIT["metre",1],
            ID["EPSG",8806]],
        PARAMETER["False northing",38744.572,
            LENGTHUNIT["metre",1],
            ID["EPSG",8807]]],
    CS[Cartesian,2],
        AXIS["northing (N)",north,
            ORDER[1],
            LENGTHUNIT["metre",1]],
        AXIS["easting (E)",east,
            ORDER[2],
            LENGTHUNIT["metre",1]],
    USAGE[
        SCOPE["Cadastre, engineering survey, topographic mapping."],
        AREA["Singapore - onshore and offshore."],
        BBOX[1.13,103.59,1.47,104.07]],
    ID["EPSG",3414]]

get_prox function will be called to get the proximity between the HDB resale flats and good primary schools

rs_coords_sf <- get_prox(rs_coords_sf, goodpri_sf, "PROX_GOOD_PRISCH")

4.3 Filtering dataset

rs_coords_sf_training <-  filter(rs_coords_sf ,flat_type == "3 ROOM") %>% 
              filter(month >= "2021-01" & month <= "2023-02")

4.3.1 Writing to RDS file

rs_factors_rds_training <- write_rds(rs_coords_sf_training, "Data/rds/rs_factors_training.rds")

5. Import Data for Analysis

5.1 Geospatial Data

Here, st_read of sf package to read simple features or layers from file.

mpsz_sf <- st_read(dsn = "Data/Geospatial", layer="MPSZ-2019")
Reading layer `MPSZ-2019' from data source 
  `C:\Users\kwekm\Desktop\SMU Year 3 Semester 2\IS415 Geospatial Analytics and Applications\KMRCrazyDuck\IS415-KMR-2\IS415_Geospatial\Take_home_ex\Data\Geospatial' 
  using driver `ESRI Shapefile'
Simple feature collection with 332 features and 6 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 103.6057 ymin: 1.158699 xmax: 104.0885 ymax: 1.470775
Geodetic CRS:  WGS 84
mpsz_sf <- st_transform(mpsz_sf, 3414)

R object used to contain the imported MPSZ-2019 shapefile is called mpsz_sf and it is a simple feature object. The correct EPSG code for SVY21 should be 3414 therefore a transformation is done above.

5.1.1 Check for invalid geometry and handle it

length(which(st_is_valid(mpsz_sf) == FALSE))
[1] 6

Making the invalid geometry valid by using st_make_valid function

mpsz_sf <- st_make_valid(mpsz_sf)
length(which(st_is_valid(mpsz_sf) == FALSE))
[1] 0

Now the geometry is valid.

5.2 Aspatial Data for HDB resale location factors

rs_factors_training <- read_rds("Data/rds/rs_factors_training.rds")

5.3 Extract storey_order due to character type

storeys <- sort(unique(rs_factors_training$storey_range))
# Create dataframe storey_range_order to store order of storey_range
storey_order <- 1:length(storeys)
storey_range_order <- data.frame(storeys, storey_order)

# Combine storey_order with resale dataframe for training and testing data
rs_factors_training <- left_join(rs_factors_training, storey_range_order, by= c("storey_range" = "storeys"))

5.4 Selecting required columns for analysis

rs_factors_training <- rs_factors_training %>%
  select(resale_price, floor_area_sqm, storey_order, remaining_lease_mths,
         PROX_CBD, PROX_ELDERLYCARE, PROX_HAWKER, PROX_MRT, PROX_PARK, PROX_GOOD_PRISCH, PROX_MALL, PROX_CHAS,
         PROX_SUPERMARKET, WITHIN_350M_KINDERGARTEN, WITHIN_350M_CHILDCARE, WITHIN_350M_BUS, WITHIN_1KM_PRISCH)

6. Exploratory Data Analysis

6.1 HDB 3 room resale prices in Histogram

ggplot(data=rs_factors_training, aes(x=`resale_price`)) +
  geom_histogram(bins=20, color="black", fill="light blue")

The results shown above reveals a right skewed distribution. This means that more resale HDB units were transacted at relative lower prices about $500,000 majority. Statistically, the skewed distribution can be normalised by using log transformation which we will be doing in the next section.

6.2 Normalise using Log Transformation of HDB 3 room resale prices in Histogram

A new variable called LOG_RESALE_PRICE by using a log transformation on the variable resale_price

rs_factors_training <- rs_factors_training %>%
  mutate(`LOG_SELLING_PRICE` = log(resale_price))

Replot Histogram of LOG_RESALE_PRICE

ggplot(data=rs_factors_training, aes(x=`LOG_SELLING_PRICE`)) +
  geom_histogram(bins=20, color="black", fill="green")

The distribution is now less skewed after the transformation.

6.3 Various histogram distribution of structural variables

Here we will consider some structural factors involved such as floor_area_sqm, storey_order, remaining_lease_mths

s_factor <- c("floor_area_sqm","remaining_lease_mths", "storey_order")

A list will be created to store the histogram structural variables

s_factor_hist_list <- vector(mode = "list", length = length(s_factor))
for (i in 1:length(s_factor)) {
  hist_plot <- ggplot(rs_factors_training, aes_string(x = s_factor[[i]])) +
    geom_histogram(color="firebrick", fill = "light coral") +
    labs(title = s_factor[[i]]) +
    theme(plot.title = element_text(size = 10),
          axis.title = element_blank())
  
  s_factor_hist_list[[i]] <- hist_plot
}

Plotting histogram to visualise distribution of structural variables

ggarrange(plotlist = s_factor_hist_list,
          ncol = 2,
          nrow = 2)

From the histogram results, only floor_area_sqm seem to resemble a normal distribution. The histogram of storey_order is skewed towards the right which means that the resale HDBs in this period and flat_type are generally on the lower levels. For the remaining_lease_mths, there are a few pattern found of popular transactions involving ~ 600 to 800 months and ~ 1100 months. This shows that more transactions are done on resale flats that have minimum 66 years lease remaining.

6.4 Various histogram of location variables

Extraction of location factors column names to plot

l_factor <- c("PROX_CBD", "PROX_ELDERLYCARE", "PROX_HAWKER", "PROX_MRT", "PROX_PARK", "PROX_GOOD_PRISCH", "PROX_MALL", "PROX_CHAS",
              "PROX_SUPERMARKET", "WITHIN_350M_KINDERGARTEN", "WITHIN_350M_CHILDCARE", "WITHIN_350M_BUS", "WITHIN_1KM_PRISCH")

Similarly, a list will be created to store the histogram location variables

l_factor_hist_list <- vector(mode = "list", length = length(l_factor))
for (i in 1:length(l_factor)) {
  hist_plot <- ggplot(rs_factors_training, aes_string(x = l_factor[[i]])) +
    geom_histogram(color="midnight blue", fill = "light sky blue") +
    labs(title = l_factor[[i]]) +
    theme(plot.title = element_text(size = 10),
          axis.title = element_blank())
  
  l_factor_hist_list[[i]] <- hist_plot
}

Plotting histogram to visualise distribution of location variables

ggarrange(plotlist = l_factor_hist_list,
          ncol = 3,
          nrow = 5)

PROX_GOOD_PRISCH have some patterns involved whereby there more transactions happening within the proximity of approximately 3km. PROX_CBD resemble somewhat a normal distribution.

Other variables like PROX_SUPERMARKET, PROX_HAWKER, PROX_MRT, PROX_MALL, PROX_CHAS, WITHIN_350M_KINDERGARTEN, WITHIN_350M_CHILDCARE is skewed towards the right. This shows that there are more resale transactions involved and residents prefer to stay near resale flats with these amenities.

6.5 Statistical Point Map

tmap_mode("view")
tm_shape(rs_factors_training) +  
  tm_dots(col = "resale_price",
          alpha = 0.6,
          style="quantile") +
  tm_view(set.zoom.limits = c(11,14)) +
tm_basemap("OpenStreetMap")
tmap_mode("plot")

From the interactive map, we can see that 3 room resale HDBs around the North-East, South and Central tend to have higher resale prices which is indicated by the darker orange points. This is in comparison to the lighter yellow points concentrated around the North and East area.

7. Data Sampling of Training and Testing Data

The entire data are split into training and test data sets with 2021-01 to 2022-12 being the training set and 2023-01 to 2023-02 being the testing set. This can be done by using initial_split() of rsample package. Rsample is one of the package of tidymodels.

Due to time constraint, 6 months (2022-07 to 2022-12) of dataset will be used to perform the training instead.s

set.seed(1234)
rs_coords_sf <-  filter(rs_factors_rds_training,flat_type == "3 ROOM") %>% 
              filter(month >= "2022-07" & month <= "2023-02")
resale_split <- initial_split(rs_coords_sf[,8:26], 
                              prop = 7.5/10,)

train_data <- training(resale_split)
test_data <- testing(resale_split)

7.1 Computing Correlation Matrix

rs_coords_sf_nogeo <- rs_coords_sf %>%
  st_drop_geometry()
corrplot::corrplot(cor(rs_coords_sf_nogeo[, 14:26]), 
                   diag = FALSE, 
                   order = "AOE",
                   tl.pos = "td", 
                   tl.cex = 0.5, 
                   method = "number", 
                   type = "upper")

The correlation matrix above shows that all the correlation values are below 0.8 with the exception of WITHIN_350M_CHILDCARE that has a value of 0.92. This shows that WITHIN_350M_CHILDCARE has a sign of multicolinearity with WTIHIN_350M_KINDERGARTEN

7.2 Building a non-spatial multiple linear regression

price_mlr <- lm(resale_price ~ floor_area_sqm +
                  remaining_lease_mths +
                  PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER +
                  PROX_MRT + PROX_PARK + PROX_MALL + 
                  PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
                  WITHIN_350M_CHILDCARE + WITHIN_350M_BUS +
                  WITHIN_1KM_PRISCH,
                data=train_data)
summary(price_mlr)

Call:
lm(formula = resale_price ~ floor_area_sqm + remaining_lease_mths + 
    PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT + PROX_PARK + 
    PROX_MALL + PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN + 
    WITHIN_350M_CHILDCARE + WITHIN_350M_BUS + WITHIN_1KM_PRISCH, 
    data = train_data)

Residuals:
    Min      1Q  Median      3Q     Max 
-211394  -30117   -4021   24521  450135 

Coefficients:
                           Estimate Std. Error t value Pr(>|t|)    
(Intercept)              -1.194e+05  1.106e+04 -10.800  < 2e-16 ***
floor_area_sqm            5.147e+03  1.461e+02  35.224  < 2e-16 ***
remaining_lease_mths      3.731e+02  5.082e+00  73.408  < 2e-16 ***
PROX_CBD                 -9.376e+03  2.507e+02 -37.395  < 2e-16 ***
PROX_ELDERLYCARE         -7.586e+03  1.729e+03  -4.387 1.18e-05 ***
PROX_HAWKER              -9.955e+03  2.309e+03  -4.312 1.67e-05 ***
PROX_MRT                 -1.342e+04  2.437e+03  -5.506 3.96e-08 ***
PROX_PARK                -1.169e+04  2.399e+03  -4.874 1.15e-06 ***
PROX_MALL                -1.252e+04  2.599e+03  -4.816 1.53e-06 ***
PROX_SUPERMARKET          3.320e+04  4.709e+03   7.051 2.15e-12 ***
WITHIN_350M_KINDERGARTEN  6.847e+03  1.109e+03   6.173 7.53e-10 ***
WITHIN_350M_CHILDCARE    -1.007e+04  1.358e+03  -7.420 1.48e-13 ***
WITHIN_350M_BUS           1.290e+03  3.400e+02   3.794 0.000151 ***
WITHIN_1KM_PRISCH        -1.761e+03  6.760e+02  -2.605 0.009239 ** 
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Residual standard error: 48560 on 3330 degrees of freedom
Multiple R-squared:  0.6869,    Adjusted R-squared:  0.6857 
F-statistic:   562 on 13 and 3330 DF,  p-value: < 2.2e-16

The building of a non-spatial multiple linear regression adjusted R square value of 0.6857 depicts that the resale prices are accurate as of the 6 months training data. Upon research adjusted R square value of 0.5 to 0.74 is a good value whereas 0.75 and above is a very good value.

7.3 Geographically Weighted Regression Predictive Method

Calibrating a model to predict HDB resale price by using geographically weighted regression method of GWmodel package.

Converting the sf data.frame to SpatialPointDataFrame

train_data_sp <- as_Spatial(train_data)
train_data_sp
class       : SpatialPointsDataFrame 
features    : 3344 
extent      : 11807.06, 45154.27, 28097.64, 48622.47  (xmin, xmax, ymin, ymax)
crs         : +proj=tmerc +lat_0=1.36666666666667 +lon_0=103.833333333333 +k=1 +x_0=28001.642 +y_0=38744.572 +ellps=WGS84 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs 
variables   : 18
names       : floor_area_sqm, flat_model, lease_commence_date, remaining_lease_mths, resale_price, postal,     PROX_ELDERLYCARE,           PROX_MRT,        PROX_HAWKER,          PROX_PARK,     PROX_SUPERMARKET,            PROX_CHAS, WITHIN_350M_KINDERGARTEN, WITHIN_350M_CHILDCARE, WITHIN_350M_BUS, ... 
min values  :             51,       DBSS,                1967,                  516,       245000, 050004, 3.47634848564088e-09, 0.0391289507571673, 0.0069808810867684, 0.0458411990294921, 1.46885170959002e-09, 2.99641325933321e-09,                        0,                     0,               0, ... 
max values  :            155,    Terrace,                2019,                 1150,      1068000, 824601,     4.78082950913573,   3.51571661813739,   2.46908408139604,   2.37596217173297,     3.31803184483494,     2.71421661000055,                       12,                    12,              16, ... 

Computing adaptive bandwidth

Next, bw.gwr() of GWmodel package will be used to determine the optimal bandwidth to be used

bw_adaptive <- bw.gwr(resale_price ~ floor_area_sqm +
                  remaining_lease_mths +
                  PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER +
                  PROX_MRT + PROX_PARK + PROX_MALL + 
                  PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
                  WITHIN_350M_CHILDCARE + WITHIN_350M_BUS +
                  WITHIN_1KM_PRISCH,
                  data=train_data_sp,
                  approach="CV",
                  kernel="gaussian",
                  adaptive=TRUE,
                  longlat=FALSE)
Take a cup of tea and have a break, it will take a few minutes.
          -----A kind suggestion from GWmodel development group
Adaptive bandwidth: 2074 CV score: 7.290983e+12 
Adaptive bandwidth: 1290 CV score: 6.832703e+12 
Adaptive bandwidth: 804 CV score: 6.274639e+12 
Adaptive bandwidth: 505 CV score: 5.595452e+12 
Adaptive bandwidth: 319 CV score: 4.806292e+12 
Adaptive bandwidth: 205 CV score: 4.455077e+12 
Adaptive bandwidth: 133 CV score: 4.276869e+12 
Adaptive bandwidth: 90 CV score: 4.215236e+12 
Adaptive bandwidth: 62 CV score: 4.118726e+12 
Adaptive bandwidth: 46 CV score: 4.051663e+12 
Adaptive bandwidth: 34 CV score: 3.942433e+12 
Adaptive bandwidth: 29 CV score: 3.81489e+12 
Adaptive bandwidth: 23 CV score: 5.615447e+12 
Adaptive bandwidth: 29 CV score: 3.81489e+12 

The result shows that 29 neighbour points will be the optimal bandwidth to be used if adaptive bandwidth is used for this data set.

Constructing the adaptive bandwidth GWR model

gwr_adaptive <- gwr.basic(formula = resale_price ~
                            floor_area_sqm +
                            remaining_lease_mths + PROX_CBD + 
                            PROX_ELDERLYCARE + PROX_HAWKER +
                            PROX_MRT + PROX_PARK + PROX_MALL + 
                            PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
                            WITHIN_350M_CHILDCARE + WITHIN_350M_BUS +
                            WITHIN_1KM_PRISCH,
                          data=train_data_sp,
                          bw=bw_adaptive, 
                          kernel = 'gaussian', 
                          adaptive=TRUE,
                          longlat = FALSE)

gwr_adaptive
   ***********************************************************************
   *                       Package   GWmodel                             *
   ***********************************************************************
   Program starts at: 2023-03-22 20:26:23 
   Call:
   gwr.basic(formula = resale_price ~ floor_area_sqm + remaining_lease_mths + 
    PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT + PROX_PARK + 
    PROX_MALL + PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN + 
    WITHIN_350M_CHILDCARE + WITHIN_350M_BUS + WITHIN_1KM_PRISCH, 
    data = train_data_sp, bw = bw_adaptive, kernel = "gaussian", 
    adaptive = TRUE, longlat = FALSE)

   Dependent (y) variable:  resale_price
   Independent variables:  floor_area_sqm remaining_lease_mths PROX_CBD PROX_ELDERLYCARE PROX_HAWKER PROX_MRT PROX_PARK PROX_MALL PROX_SUPERMARKET WITHIN_350M_KINDERGARTEN WITHIN_350M_CHILDCARE WITHIN_350M_BUS WITHIN_1KM_PRISCH
   Number of data points: 3344
   ***********************************************************************
   *                    Results of Global Regression                     *
   ***********************************************************************

   Call:
    lm(formula = formula, data = data)

   Residuals:
    Min      1Q  Median      3Q     Max 
-211394  -30117   -4021   24521  450135 

   Coefficients:
                              Estimate Std. Error t value Pr(>|t|)    
   (Intercept)              -1.194e+05  1.106e+04 -10.800  < 2e-16 ***
   floor_area_sqm            5.147e+03  1.461e+02  35.224  < 2e-16 ***
   remaining_lease_mths      3.731e+02  5.082e+00  73.408  < 2e-16 ***
   PROX_CBD                 -9.376e+03  2.507e+02 -37.395  < 2e-16 ***
   PROX_ELDERLYCARE         -7.586e+03  1.729e+03  -4.387 1.18e-05 ***
   PROX_HAWKER              -9.955e+03  2.309e+03  -4.312 1.67e-05 ***
   PROX_MRT                 -1.342e+04  2.437e+03  -5.506 3.96e-08 ***
   PROX_PARK                -1.169e+04  2.399e+03  -4.874 1.15e-06 ***
   PROX_MALL                -1.252e+04  2.599e+03  -4.816 1.53e-06 ***
   PROX_SUPERMARKET          3.320e+04  4.709e+03   7.051 2.15e-12 ***
   WITHIN_350M_KINDERGARTEN  6.847e+03  1.109e+03   6.173 7.53e-10 ***
   WITHIN_350M_CHILDCARE    -1.007e+04  1.358e+03  -7.420 1.48e-13 ***
   WITHIN_350M_BUS           1.290e+03  3.400e+02   3.794 0.000151 ***
   WITHIN_1KM_PRISCH        -1.761e+03  6.760e+02  -2.605 0.009239 ** 

   ---Significance stars
   Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 
   Residual standard error: 48560 on 3330 degrees of freedom
   Multiple R-squared: 0.6869
   Adjusted R-squared: 0.6857 
   F-statistic:   562 on 13 and 3330 DF,  p-value: < 2.2e-16 
   ***Extra Diagnostic information
   Residual sum of squares: 7.852292e+12
   Sigma(hat): 48472.45
   AIC:  81673.03
   AICc:  81673.17
   BIC:  78542.47
   ***********************************************************************
   *          Results of Geographically Weighted Regression              *
   ***********************************************************************

   *********************Model calibration information*********************
   Kernel function: gaussian 
   Adaptive bandwidth: 29 (number of nearest neighbours)
   Regression points: the same locations as observations are used.
   Distance metric: Euclidean distance metric is used.

   ****************Summary of GWR coefficient estimates:******************
                                   Min.     1st Qu.      Median     3rd Qu.
   Intercept                -4002746.15  -235385.18   -34580.53   165740.37
   floor_area_sqm              -9039.22     2468.61     3478.12     4521.92
   remaining_lease_mths        -3341.30      216.38      340.78      476.75
   PROX_CBD                  -864707.56   -19759.97    -5747.23     7298.99
   PROX_ELDERLYCARE          -268369.12   -10011.36     8897.91    31527.65
   PROX_HAWKER               -419047.98   -27256.08    -2321.53    31981.66
   PROX_MRT                  -726178.71   -63311.44   -29014.31    -5403.82
   PROX_PARK                 -224999.73   -31675.12     -813.88    29170.51
   PROX_MALL                 -442346.01   -32012.80     -422.88    28907.98
   PROX_SUPERMARKET          -605058.66   -38749.97    -7085.90    27400.11
   WITHIN_350M_KINDERGARTEN   -59384.38    -4390.15      655.91     5495.98
   WITHIN_350M_CHILDCARE     -122685.42    -6761.37    -1476.42     4058.72
   WITHIN_350M_BUS            -14982.92    -1402.20      427.62     2017.20
   WITHIN_1KM_PRISCH         -120872.39    -7271.14    -2095.86     2626.12
                                  Max.
   Intercept                4283030.08
   floor_area_sqm             13792.84
   remaining_lease_mths         712.02
   PROX_CBD                  242427.63
   PROX_ELDERLYCARE          834134.48
   PROX_HAWKER               761019.46
   PROX_MRT                  401956.58
   PROX_PARK                 612149.88
   PROX_MALL                 816078.70
   PROX_SUPERMARKET          727238.97
   WITHIN_350M_KINDERGARTEN  108684.15
   WITHIN_350M_CHILDCARE     192033.36
   WITHIN_350M_BUS            20699.66
   WITHIN_1KM_PRISCH        1316472.19
   ************************Diagnostic information*************************
   Number of data points: 3344 
   Effective number of parameters (2trace(S) - trace(S'S)): 746.3561 
   Effective degrees of freedom (n-2trace(S) + trace(S'S)): 2597.644 
   AICc (GWR book, Fotheringham, et al. 2002, p. 61, eq 2.33): 78546.19 
   AIC (GWR book, Fotheringham, et al. 2002,GWR p. 96, eq. 4.22): 77649.12 
   BIC (GWR book, Fotheringham, et al. 2002,GWR p. 61, eq. 2.34): 78685.47 
   Residual sum of squares: 1.978519e+12 
   R-square value:  0.9211137 
   Adjusted R-square value:  0.8984393 

   ***********************************************************************
   Program stops at: 2023-03-22 20:26:29 

7.4 Preparing Coordinates Data

The code chunk below extract the x,y coordinates of the full, training and test data sets.

coords <- st_coordinates(rs_coords_sf)
coords_train <- st_coordinates(train_data)
coords_test <- st_coordinates(test_data)

Dropping Geometry Field

We will drop geometry column of the sf data.frame by using st_drop_geometry() of sf package.

train_data <- train_data %>% 
  st_drop_geometry()

7.5 Calibrating Random Forest Model

In this section, calibration of a model to predict HDB resale price will be done by using random forest function of ranger package.

set.seed(1234)
rf <- ranger(resale_price ~ floor_area_sqm + 
               remaining_lease_mths + PROX_CBD + PROX_ELDERLYCARE + 
               PROX_HAWKER + PROX_MRT + PROX_PARK + PROX_MALL + 
               PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
               WITHIN_350M_CHILDCARE + WITHIN_350M_BUS + 
               WITHIN_1KM_PRISCH,
             data=train_data)
print(rf)
Ranger result

Call:
 ranger(resale_price ~ floor_area_sqm + remaining_lease_mths +      PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT + PROX_PARK +      PROX_MALL + PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +      WITHIN_350M_CHILDCARE + WITHIN_350M_BUS + WITHIN_1KM_PRISCH,      data = train_data) 

Type:                             Regression 
Number of trees:                  500 
Sample size:                      3344 
Number of independent variables:  13 
Mtry:                             3 
Target node size:                 5 
Variable importance mode:         none 
Splitrule:                        variance 
OOB prediction error (MSE):       905538392 
R squared (OOB):                  0.8793007 

7.6 Calibrating Geographical Random Forest Model

In this section, calibration of a model to predict HDB resale price will be done by using grf() of SpatialML package.

7.6.1 Calibrating using training data.

The code chunk below calibrate a geographic random forest model by using grf() of SpatialML package.

set.seed(1234)
gwRF_adaptive <- grf(formula = resale_price ~ floor_area_sqm +
                       remaining_lease_mths + PROX_CBD + PROX_ELDERLYCARE +
                       PROX_HAWKER + PROX_MRT + PROX_PARK + PROX_MALL +
                       PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +
                       WITHIN_350M_CHILDCARE + WITHIN_350M_BUS +
                       WITHIN_1KM_PRISCH,
                     dframe=train_data, 
                     bw=29,
                     kernel="adaptive",
                     coords=coords_train)
Ranger result

Call:
 ranger(resale_price ~ floor_area_sqm + remaining_lease_mths +      PROX_CBD + PROX_ELDERLYCARE + PROX_HAWKER + PROX_MRT + PROX_PARK +      PROX_MALL + PROX_SUPERMARKET + WITHIN_350M_KINDERGARTEN +      WITHIN_350M_CHILDCARE + WITHIN_350M_BUS + WITHIN_1KM_PRISCH,      data = train_data, num.trees = 500, mtry = 4, importance = "impurity",      num.threads = NULL) 

Type:                             Regression 
Number of trees:                  500 
Sample size:                      3344 
Number of independent variables:  13 
Mtry:                             4 
Target node size:                 5 
Variable importance mode:         impurity 
Splitrule:                        variance 
OOB prediction error (MSE):       877913319 
R squared (OOB):                  0.8829828 
          floor_area_sqm     remaining_lease_mths                 PROX_CBD 
            3.485195e+12             9.676867e+12             4.463366e+12 
        PROX_ELDERLYCARE              PROX_HAWKER                 PROX_MRT 
            6.862616e+11             1.141946e+12             1.279920e+12 
               PROX_PARK                PROX_MALL         PROX_SUPERMARKET 
            7.806976e+11             8.436208e+11             8.442072e+11 
WITHIN_350M_KINDERGARTEN    WITHIN_350M_CHILDCARE          WITHIN_350M_BUS 
            2.392342e+11             3.541417e+11             2.951565e+11 
       WITHIN_1KM_PRISCH 
            4.565918e+11 
     Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
-402499.9  -12023.0       0.0     785.4   12428.5  563333.3 
      Min.    1st Qu.     Median       Mean    3rd Qu.       Max. 
-103509.54   -5414.88      47.79     -13.69    5398.42  117898.51 
                               Min          Max        Mean         StD
floor_area_sqm                   0 758092514666 10643196179 39250160257
remaining_lease_mths     183971887 326651950547 17782852544 37750905453
PROX_CBD                 112700112 161599123721  5873361736 14095584591
PROX_ELDERLYCARE         105909592 794019226251  7880035687 29149152061
PROX_HAWKER               85193839 159103694735  5802642716 13003033185
PROX_MRT                 118739325 502912861663  7273560584 25247348114
PROX_PARK                 73741541 533473497989  7947511107 31281279139
PROX_MALL                 66485659 531205756333  7014653778 23201695336
PROX_SUPERMARKET          72866122 732610819724  7668882828 36636800002
WITHIN_350M_KINDERGARTEN  10413824 190747335369  2905764793  8167628164
WITHIN_350M_CHILDCARE      8664958  69580318613  2109829659  5550231441
WITHIN_350M_BUS           13322854 147242251064  4547292062 14041512487
WITHIN_1KM_PRISCH                0 172176021904  1615868728  7339020997
write_rds(gwRF_adaptive, "Data/rds/gwRF_adaptive.rds")
gwRF_adaptive <- read_rds("Data/rds/gwRF_adaptive.rds")

7.6.2 Predicting using Test Data

The code chunk below will be used to combine the test data with its corresponding coordinates data.

Preparing the test data

test_data <- cbind(test_data, coords_test) %>%
  st_drop_geometry()

Predicting with test data

Next, predict.grf() of spatialML package will be used to predict the resale value by using the test data and gwRF_adaptive model calibrated earlier.

gwRF_pred <- predict.grf(gwRF_adaptive, 
                           test_data, 
                           x.var.name="X",
                           y.var.name="Y", 
                           local.w=1,
                           global.w=0)

Before moving on, let us save the output into rds file for future use.

GRF_pred <- write_rds(gwRF_pred, "Data/rds/GRF_pred.rds")
GRF_pred
   [1] 360816.7 341290.6 427349.3 354374.6 359263.6 341442.2 378349.9 346453.2
   [9] 372848.0 346218.7 393847.3 330879.4 361145.6 359014.6 351353.5 352994.8
  [17] 331388.8 384135.3 556454.4 374157.9 359783.7 356438.4 400242.3 356541.8
  [25] 409777.9 409777.9 339625.0 415623.8 422819.6 358891.7 348004.7 357050.4
  [33] 375936.6 347277.8 358124.9 712866.2 355741.6 682850.8 288122.7 516808.9
  [41] 406070.5 351457.4 378553.5 452245.5 353898.0 369438.0 385164.3 608304.0
  [49] 377400.3 429008.3 571493.6 304596.7 548176.6 391028.6 343283.4 383210.1
  [57] 356663.0 468883.1 496159.3 379751.9 298526.7 277462.8 281575.2 366846.9
  [65] 340921.0 424142.1 385614.6 334611.4 344933.5 279527.4 687564.9 338133.9
  [73] 511111.3 482226.1 482226.1 673330.0 520433.6 379722.5 453655.4 437877.6
  [81] 435063.9 442606.5 455091.6 406215.6 436666.6 576443.2 354866.6 318265.2
  [89] 411242.9 446876.9 444136.5 427075.3 443197.1 386515.1 374404.3 394786.8
  [97] 390463.2 516388.1 517858.4 396946.3 396350.1 394965.8 406750.1 381590.5
 [105] 414228.9 368249.5 329828.4 319806.8 329828.4 699407.5 291879.6 320215.1
 [113] 335442.6 328879.6 335937.5 293227.1 391928.3 318102.8 314988.2 411391.1
 [121] 414529.6 405120.1 413092.2 363431.2 391976.7 378621.6 365479.0 350247.8
 [129] 380432.3 361700.0 402405.6 596851.8 343383.0 420766.3 370393.1 345119.2
 [137] 388331.3 385855.2 393103.7 368871.5 382953.1 360183.9 366081.4 471878.5
 [145] 387012.8 379817.6 363654.5 369690.5 361618.7 335739.7 348637.4 384135.4
 [153] 361137.3 361394.9 345864.0 333237.0 341486.3 364468.6 368730.9 501326.5
 [161] 500470.5 408590.0 616683.2 432712.5 404215.6 632175.4 378524.5 423277.3
 [169] 406646.0 375916.7 405705.6 362944.9 454698.8 385172.0 361650.9 385292.8
 [177] 575186.8 335580.8 359577.1 392405.7 282801.4 302112.0 280257.6 331336.2
 [185] 399524.1 367223.2 336399.0 325512.0 374504.4 364286.3 364174.4 352147.6
 [193] 365903.1 439512.4 332554.6 355957.8 377869.4 388603.5 301335.9 411233.2
 [201] 344114.6 422213.0 318743.5 687564.9 674004.1 418537.6 511111.3 303445.4
 [209] 917719.9 527608.0 580954.8 333297.7 334728.7 492027.6 404303.7 394476.8
 [217] 423264.8 438684.8 431977.5 439392.0 435063.9 438342.9 448166.0 476862.1
 [225] 447230.7 553636.2 358791.9 399755.6 377005.9 534451.6 405112.1 432770.0
 [233] 414643.1 445350.3 445350.3 457615.3 357209.3 446365.7 393276.1 420779.3
 [241] 399770.7 392210.0 386418.9 376801.7 426892.5 686548.8 314469.2 335871.6
 [249] 322544.0 372527.1 295761.0 322243.0 308091.0 387279.5 330593.4 454966.6
 [257] 454966.6 442945.8 454966.6 365887.1 379890.3 357769.4 416485.1 349781.4
 [265] 381301.8 458249.3 347249.0 349357.8 427444.1 416757.0 543986.9 356468.5
 [273] 356921.4 411854.9 399103.9 411854.9 368349.9 390734.7 376081.8 371980.3
 [281] 355361.7 394304.3 359177.5 417398.0 330879.4 297012.4 367880.7 301107.5
 [289] 383160.1 355844.1 318539.9 350946.7 354279.8 349196.1 383029.7 354955.0
 [297] 409521.6 349350.3 359414.7 395183.5 469238.5 437941.3 332375.0 342971.1
 [305] 356082.0 354253.9 389000.0 363713.7 494819.4 357044.4 381481.4 368957.0
 [313] 501502.7 500470.5 528050.1 546567.6 580240.5 735085.5 306203.0 382306.5
 [321] 743598.0 365402.3 382252.7 348017.0 450379.6 343909.9 314091.6 514510.3
 [329] 477435.7 455260.6 542234.2 398175.1 393883.7 292437.7 292220.4 281036.6
 [337] 434707.9 449544.8 286266.3 328833.4 301889.3 323053.0 308740.5 385231.4
 [345] 367223.2 341285.5 317518.5 375900.9 341221.4 425395.2 371311.3 390864.5
 [353] 477979.2 492341.4 350612.3 356493.1 306151.5 381796.2 423064.8 410923.9
 [361] 410923.9 687564.9 385102.8 368741.5 917785.1 527608.0 561589.7 391778.2
 [369] 491707.5 464569.8 464569.8 473648.4 432681.6 476862.1 434192.6 368628.7
 [377] 324163.6 631189.8 673227.8 415670.0 373140.8 368946.0 318960.0 326882.2
 [385] 345402.6 333935.1 631688.2 403100.3 455203.4 441325.7 434360.0 455923.0
 [393] 392802.3 398760.5 427327.1 362336.8 397905.9 391708.2 398090.5 403022.8
 [401] 409419.4 364343.2 359758.1 414330.7 406843.7 289402.9 318924.3 699173.5
 [409] 340133.0 704653.4 684678.3 346004.8 318924.3 726661.0 342280.2 310922.4
 [417] 359743.4 475539.6 295881.0 417310.9 414905.8 414481.8 380793.7 358652.1
 [425] 350793.3 374978.3 345987.9 361033.4 343378.0 369679.2 430184.6 325146.3
 [433] 374101.9 377171.5 353013.2 373747.1 359884.9 337821.0 347980.0 366755.1
 [441] 387391.0 353300.8 380603.3 378810.6 354063.8 361401.5 375025.8 364772.5
 [449] 359788.5 407206.7 344919.4 328080.3 361331.4 446337.8 367964.5 353963.0
 [457] 353151.6 349280.6 444956.3 780899.1 306203.0 599761.6 391374.9 345207.8
 [465] 654828.7 379755.7 415162.1 422593.9 422378.2 419442.9 416611.1 394068.5
 [473] 454698.8 397317.2 532771.6 359325.9 364207.1 378332.8 360937.8 288615.0
 [481] 281036.6 431045.2 314916.5 551992.1 323059.5 333267.9 364016.0 365101.1
 [489] 439512.4 371451.0 368848.5 422869.0 316133.9 299458.3 365695.6 353476.4
 [497] 395101.0 276585.6 366916.4 338438.7 904638.7 681788.3 681788.3 333335.5
 [505] 418381.1 402906.0 454918.0 450983.8 435573.4 437456.9 445191.9 293029.1
 [513] 618035.3 664542.9 664542.9 396256.6 402856.4 447135.1 462638.5 369225.9
 [521] 623896.0 438302.7 461486.9 435734.7 446340.3 454020.9 464875.9 405858.7
 [529] 436670.3 421516.0 444939.9 382797.2 371697.3 343947.8 342779.4 487565.6
 [537] 400988.8 433009.1 429074.5 382302.0 398135.3 290052.2 342125.5 341868.0
 [545] 343906.7 371489.1 365006.3 277138.7 319404.5 337443.9 333176.9 332904.9
 [553] 453362.4 414699.5 414589.1 359083.0 382199.8 358075.7 347442.3 347796.2
 [561] 373967.1 389621.5 411256.5 382710.3 377209.1 385032.5 393084.6 377822.9
 [569] 396382.7 394096.1 392773.0 346922.9 367361.5 652763.8 376265.7 328958.1
 [577] 353706.8 555233.0 359788.5 356541.8 530655.0 338762.1 416402.2 316925.6
 [585] 362187.0 494819.4 346298.3 410914.8 300298.8 323506.0 382705.3 352887.6
 [593] 432209.1 308274.2 383332.0 379113.7 357338.4 366456.7 396840.9 459830.0
 [601] 441178.4 455418.7 378378.6 444354.7 352431.1 431045.2 301041.6 288633.6
 [609] 549028.0 320376.6 377664.2 348160.2 323059.5 386673.2 386854.7 314834.4
 [617] 360061.3 359435.2 373635.1 517039.0 472082.6 361190.8 371451.0 367091.4
 [625] 329780.6 347594.3 274351.7 340319.4 351851.4 473739.1 368741.5 338438.7
 [633] 283253.9 331691.2 555215.0 341201.3 403467.3 347189.3 333355.5 333355.5
 [641] 448259.9 453806.5 438135.2 414839.2 453017.5 453651.7 453651.7 432748.0
 [649] 482656.1 412523.9 640485.1 659449.1 405730.0 350405.0 436853.2 441364.9
 [657] 447645.5 434260.9 459965.9 398344.1 518791.5 390574.6 413718.9 402707.7
 [665] 414723.8 380429.0 385430.7 416819.1 432955.2 429870.9 399203.5 413512.8
 [673] 363847.7 336880.9 347535.4 686965.8 326233.7 321068.2 322779.7 307749.7
 [681] 297904.7 319767.5 332810.9 418355.2 377674.7 386909.4 385846.1 353414.5
 [689] 346899.9 370985.0 359894.4 360315.6 431172.1 417970.9 596357.8 381956.4
 [697] 385748.3 351031.6 356707.9 398064.2 346906.1 378376.0 367344.4 371640.1
 [705] 390434.2 387103.6 360931.6 367106.1 361681.8 508940.9 394556.4 459705.7
 [713] 345292.4 357351.2 348500.4 377383.8 339878.7 500614.3 687960.2 427609.9
 [721] 334994.0 373959.3 348271.9 374512.5 313433.2 371862.7 411420.1 381413.3
 [729] 352914.6 352914.6 398034.2 290478.3 317964.4 284219.2 287167.6 302211.1
 [737] 293876.1 426100.5 357395.8 333267.9 333065.1 381815.9 401986.8 397348.6
 [745] 320234.2 357120.5 365206.1 357796.8 357796.8 452114.9 340319.4 414675.2
 [753] 430610.0 370588.8 348623.4 503806.2 472479.7 411734.4 472491.3 456012.7
 [761] 480503.3 482421.1 412523.9 444507.7 300822.3 322504.8 381953.7 671172.4
 [769] 671172.4 354309.3 461234.9 404437.8 354554.0 445999.9 427017.7 430990.1
 [777] 452983.9 430645.1 430645.1 440457.9 451338.3 452889.8 455923.0 429479.6
 [785] 436441.3 378154.4 421125.8 518791.5 392743.5 400451.2 402474.5 392727.2
 [793] 389454.4 415522.9 414915.8 335375.9 343547.6 678927.6 335500.5 437034.3
 [801] 293359.6 340325.1 319006.8 306611.6 295925.2 307253.3 431135.7 438803.8
 [809] 405120.1 408614.1 399507.8 419325.1 417366.2 438038.0 436961.4 426771.1
 [817] 447846.9 339236.0 409618.1 420968.1 354677.2 338671.7 419597.4 416394.8
 [825] 374942.6 384690.7 361561.1 370593.5 321589.6 347509.6 381061.9 419059.0
 [833] 347916.9 391822.1 375932.0 331041.8 372197.2 375202.7 351053.0 323007.6
 [841] 368158.6 316730.8 332555.4 396385.2 350870.1 377582.1 420661.4 578333.9
 [849] 361794.2 369164.3 381198.3 406911.2 339755.2 355024.1 355598.4 353251.8
 [857] 515719.3 365309.8 527906.3 705014.2 306203.0 536781.9 708234.4 704532.4
 [865] 383667.0 359432.8 699853.6 371124.9 398398.2 413317.7 395805.2 476633.6
 [873] 420834.1 422079.2 406882.0 369124.6 369547.8 458382.7 411408.1 384295.5
 [881] 372350.0 414955.4 396476.9 590029.6 367863.3 368844.4 302844.5 306909.2
 [889] 297571.2 582019.1 302910.1 340097.2 548176.6 387569.6 404403.5 408945.4
 [897] 388422.6 373323.9 337703.5 375181.8 341307.5 384918.5 377882.7 361866.8
 [905] 365534.6 490307.7 442024.8 511809.5 363797.6 366684.3 413039.7 412565.4
 [913] 327438.7 398789.6 313108.1 283408.6 357394.0 454638.1 370627.0 398499.2
 [921] 370588.8 361786.7 376162.4 376162.4 367149.3 368419.0 368419.0 421201.6
 [929] 273966.6 482248.8 299645.2 375597.6 375597.6 454831.6 435922.0 467202.8
 [937] 444521.4 412787.5 491621.5 446170.3 439438.2 469657.7 322881.7 282379.6
 [945] 379469.6 379956.5 455709.9 416814.6 389123.1 418973.4 418973.4 447353.9
 [953] 437361.5 459359.8 451490.7 367848.4 402008.0 398794.3 338191.8 345596.4
 [961] 442614.5 406578.8 468604.9 391140.2 410443.4 395081.3 428018.4 411710.9
 [969] 392317.0 385776.5 474388.4 455693.7 399761.9 725030.2 339183.0 343025.4
 [977] 343025.4 293359.6 397758.5 324466.1 313428.2 300356.0 312662.5 435025.7
 [985] 431135.7 457043.2 457043.2 372757.1 417814.4 429332.5 355635.8 350361.7
 [993] 370391.2 438891.5 448000.7 374451.9 359690.9 360385.6 395695.7 624140.1
[1001] 437385.1 434742.2 394436.3 351214.4 379202.9 652513.8 611734.0 332167.2
[1009] 341810.8 415083.0 333022.3 379668.3 375928.0 582478.6 360290.8 361732.9
[1017] 357695.7 370693.4 409521.6 360143.8 454920.4 372577.3 683840.8 687960.2
[1025] 447267.6 308277.2 360078.2 349996.5 702167.3 365899.2 382110.4 411959.1
[1033] 466043.2 322975.6 388129.3 356716.1 367041.6 652887.8 367171.7 364651.4
[1041] 380894.7 595562.6 284311.4 532277.8 548176.6 373323.9 374397.6 332547.5
[1049] 351408.2 385496.4 443729.7 366397.5 365661.8 356434.6 313211.1 340587.6
[1057] 277838.4 344062.0 344481.8 359602.2 396650.2 472790.1 712086.5 354617.2
[1065] 299645.2 561487.7 331783.8 498052.0 429999.0 411989.4 439090.4 438916.9
[1073] 420775.5 435704.4 431105.0 417283.9 673380.0 453750.4 423986.4 431040.5
[1081] 426018.5 442614.5 410813.1 410443.4 400451.2 391983.7 413489.0 378235.5
[1089] 386721.8 352414.9 376466.2 328859.9 335427.6 336620.8 453772.9 318960.8
[1097] 350582.7 315765.6 322226.3 312604.5 392008.3 457043.2 436961.4 393114.8
[1105] 427360.7 373208.0 373559.6 356244.2 353656.3 407996.0 483199.7 378621.6
[1113] 375836.1 403712.2 377246.0

Converting the predicting output into a data frame

The output of the predict.grf() is a vector of predicted values. It is wiser to convert it into a data frame for further visualisation and analysis.

GRF_pred <- read_rds("Data/rds/GRF_pred.rds")
GRF_pred_df <- as.data.frame(GRF_pred)

In the code chunk below, cbind() is used to append the predicted values onto test_data

test_data_p <- cbind(test_data, GRF_pred_df)
write_rds(test_data_p, "Data/rds/test_data_p.rds")

7.6.3 Calculating Root Mean Square Error

The root mean square error (RMSE) allows us to measure how far predicted values are from observed values in a regression analysis. In the code chunk below, rmse() of Metrics package is used to compute the RMSE.

rmse(test_data_p$resale_price, 
     test_data_p$GRF_pred)
[1] 32313.2

The RMSE value of 32313.2 depicts that the geographical regression prediction model is relatively near the test data resale prices. A lower RMSE indicates a better fit.

7.6.4 Visualising the predicted values

Alternatively, scatterplot can be used to visualise the actual resale price and the predicted resale price by using the code chunk below.

ggplot(data = test_data_p,
       aes(x = GRF_pred,
           y = resale_price)) +
  geom_point()

From the scatterplot visualisation, it depicts that the actual resale price and the predicted resale prices are not far apart if we do draw a linear line. From the visualisation, it shows majority of the plots are in the scatter area of $400,000.

8. References

  • Take Home Exercise 3 (Nor Aisyah Binte Ajit) - https://aisyahajit2018-is415.netlify.app/posts/2021-11-07-take-home-exercise-3/

  • In Class Exercise 8 (Professor Kam) - https://is415-gaa-tskam.netlify.app/in-class_ex/in-class_ex08/in-class_ex08_gwr

  • In Class Exercise 9 (Professor Kam) - https://is415-gaa-tskam.netlify.app/in-class_ex/in-class_ex09/in-class_ex09_gwml

Code chunks are reviewed and referenced from senior Nor Aisyah and Professor Kam in class exercises. End of take home exercise 3.